{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pyquery"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "first item\n",
      "second item\n",
      "third item\n",
      "fourth item\n",
      "first item\n"
     ]
    }
   ],
   "source": [
    "from pyquery import PyQuery as pq\n",
    "\n",
    "html = '''<div>\n",
    "    <ul>\n",
    "        <li class=\"item-0\">first item</li>\n",
    "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
    "        <li class=\"item-0 active\"><a href=\"link3.html\">\n",
    "            <span>third item</span>\n",
    "        </a></li>\n",
    "        <li class=\"item-1 active\"><a href=\"link4.html\">fourth item</a></li>\n",
    "        <li class=\"item-0\"><a href=\"link5.html\">first item</a></li>\n",
    "    </ul>\n",
    "</div>\n",
    "'''\n",
    "\n",
    "doc = pq(html)\n",
    "for i in doc('li').items():\n",
    "    print(i.text())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查找元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 子元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pyquery.pyquery.PyQuery'>\n",
      "<li class=\"item-0\">first item</li>\n",
      "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
      "        <li class=\"item-0 active\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        <li class=\"item-1 active\"><a href=\"link4.html\">fourth item</a></li>\n",
      "        <li class=\"item-0\"><a href=\"link5.html\">first item</a></li>\n",
      "    \n",
      "<class 'pyquery.pyquery.PyQuery'>\n",
      "<a href=\"link2.html\">second item</a><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a><a href=\"link4.html\">fourth item</a><a href=\"link5.html\">first item</a>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from pyquery import PyQuery as pq\n",
    "\n",
    "html = '''<div>\n",
    "    <ul>\n",
    "        <li class=\"item-0\">first item</li>\n",
    "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
    "        <li class=\"item-0 active\"><a href=\"link3.html\">\n",
    "            <span>third item</span>\n",
    "        </a></li>\n",
    "        <li class=\"item-1 active\"><a href=\"link4.html\">fourth item</a></li>\n",
    "        <li class=\"item-0\"><a href=\"link5.html\">first item</a></li>\n",
    "    </ul>\n",
    "</div>\n",
    "'''\n",
    "\n",
    "doc = pq(html)\n",
    "li = doc('li')\n",
    "print(type(li))\n",
    "print(li)\n",
    "a = li('a')\n",
    "active = li.find('.active')\n",
    "print(type(a))\n",
    "print(a)\n",
    "print(active)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 父元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
      "        <li class=\"item-0 active\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        <li class=\"item-1 active\"><a href=\"link4.html\">fourth item</a></li>\n",
      "        <li class=\"item-0\"><a href=\"link5.html\">first item</a></li>\n",
      "    \n",
      "<ul>\n",
      "        <li class=\"item-0\">first item</li>\n",
      "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
      "        <li class=\"item-0 active\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        <li class=\"item-1 active\"><a href=\"link4.html\">fourth item</a></li>\n",
      "        <li class=\"item-0\"><a href=\"link5.html\">first item</a></li>\n",
      "    </ul>\n",
      "    \n",
      "<ul>\n",
      "        <li class=\"item-0\">first item</li>\n",
      "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
      "        <li class=\"item-0 active\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        <li class=\"item-1 active\"><a href=\"link4.html\">fourth item</a></li>\n",
      "        <li class=\"item-0\"><a href=\"link5.html\">first item</a></li>\n",
      "    </ul>\n",
      "    \n",
      "------------------------------\n",
      "<div class=\"box box3\">\n",
      "                <div class=\"box box4\">\n",
      "                    BOX\n",
      "                </div>\n",
      "            </div>\n",
      "        \n"
     ]
    }
   ],
   "source": [
    "html = '''<div>\n",
    "    <ul>\n",
    "        <li class=\"item-0\">first item</li>\n",
    "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
    "        <li class=\"item-0 active\"><a href=\"link3.html\">\n",
    "            <span>third item</span>\n",
    "        </a></li>\n",
    "        <li class=\"item-1 active\"><a href=\"link4.html\">fourth item</a></li>\n",
    "        <li class=\"item-0\"><a href=\"link5.html\">first item</a></li>\n",
    "    </ul>\n",
    "    <div class=\"box box1\">\n",
    "        <div class=\"box box2\">\n",
    "            <div class=\"box box3\">\n",
    "                <div class=\"box box4\">\n",
    "                    BOX\n",
    "                </div>\n",
    "            </div>\n",
    "        </div>\n",
    "    </div>\n",
    "</div>\n",
    "'''\n",
    "\n",
    "doc = pq(html)\n",
    "a= doc('li a')\n",
    "print(a.parent())\n",
    "print(a.parent().parent())\n",
    "print(a.parents('ul'))\n",
    "print('---'*10)\n",
    "box = doc(\".box3\")\n",
    "print(box.closest('.box'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 兄弟元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<li class=\"item-2 active\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        <li class=\"item-4 active\"><a href=\"link4.html\">fourth item</a></li>\n",
      "        \n",
      "<li class=\"item-2 active\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        \n",
      "<li class=\"item-2 active\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        \n"
     ]
    }
   ],
   "source": [
    "html = '''<div>\n",
    "    <ul>\n",
    "        <li class=\"item-0\">first item</li>\n",
    "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
    "        <li class=\"item-2 active\"><a href=\"link3.html\">\n",
    "            <span>third item</span>\n",
    "        </a></li>\n",
    "        <li class=\"item-3\"><a href=\"link2.html\">xxxxxxxxxxxxxxxxx item</a></li>\n",
    "        <li class=\"item-4 active\"><a href=\"link4.html\">fourth item</a></li>\n",
    "        <li class=\"item-5\"><a href=\"link5.html\">first item</a></li>\n",
    "    </ul>\n",
    "</div>\n",
    "'''\n",
    "\n",
    "doc = pq(html)\n",
    "li= doc('li').eq(3)\n",
    "print(li.siblings('.active'))\n",
    "# print(li.next_all())\n",
    "print(li.prev())\n",
    "print(li.prev_all('.active'))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "link3.html \n",
      " link3.html\n"
     ]
    }
   ],
   "source": [
    "html = '''<div>\n",
    "    <ul>\n",
    "        <li class=\"item-0\">first item</li>\n",
    "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
    "        <li class=\"item-2 active\"><a href=\"link3.html\">\n",
    "            <span>third item</span>\n",
    "        </a></li>\n",
    "        <li class=\"item-3\"><a href=\"link2.html\">xxxxxxxxxxxxxxxxx item</a></li>\n",
    "        <li class=\"item-4 active\"><a href=\"link4.html\">fourth item</a></li>\n",
    "        <li class=\"item-5\"><a href=\"link5.html\">first item</a></li>\n",
    "    </ul>\n",
    "</div>\n",
    "'''\n",
    "\n",
    "doc = pq(html)\n",
    "li= doc('li').eq(2)\n",
    "href = li.find('a').attr('href')\n",
    "href2 = li('a').attr.href\n",
    "print(href,'\\n',href2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "third item\n",
      "<a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a>\n"
     ]
    }
   ],
   "source": [
    "html = '''<div>\n",
    "    <ul>\n",
    "        <li class=\"item-0\">first item</li>\n",
    "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
    "        <li class=\"item-2 active\"><a href=\"link3.html\">\n",
    "            <span>third item</span>\n",
    "        </a></li>\n",
    "        <li class=\"item-3\"><a href=\"link2.html\">xxxxxxxxxxxxxxxxx item</a></li>\n",
    "        <li class=\"item-4 active\"><a href=\"link4.html\">fourth item</a></li>\n",
    "        <li class=\"item-5\"><a href=\"link5.html\">first item</a></li>\n",
    "    </ul>\n",
    "</div>\n",
    "'''\n",
    "\n",
    "doc = pq(html)\n",
    "li= doc('li').eq(2)\n",
    "print(li.text())\n",
    "print(li.html())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DOM操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<li class=\"item-2 active\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        \n",
      "<li class=\"item-2 active abc\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        \n",
      "--------------------------------------------------------------------------------\n",
      "hello\n",
      "world\n",
      "this is a programe\n",
      "hello\n",
      "world\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "html = '''<div>\n",
    "    <ul>\n",
    "        <li class=\"item-0\">first item</li>\n",
    "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
    "        <li class=\"item-2 active\"><a href=\"link3.html\">\n",
    "            <span>third item</span>\n",
    "        </a></li>\n",
    "        <li class=\"item-3\"><a href=\"link2.html\">xxxxxxxxxxxxxxxxx item</a></li>\n",
    "        <li class=\"item-4 active\"><a href=\"link4.html\">fourth item</a></li>\n",
    "        <li class=\"item-5\"><a href=\"link5.html\">first item</a></li>\n",
    "        \n",
    "    </ul>\n",
    "    \n",
    "    <div id=\"box\">\n",
    "        hello <br/> world\n",
    "        <p> this is a programe</p>\n",
    "        \n",
    "    </div>\n",
    "</div>\n",
    "'''\n",
    "doc = pq(html)\n",
    "li= doc('li').eq(2)\n",
    "print(li)\n",
    "li.addClass('abc')\n",
    "print(li)\n",
    "print('----'*20)\n",
    "box = doc('#box')\n",
    "print(box.text())\n",
    "box.find('p').remove() # box.remove('p')\n",
    "print(box.text())\n",
    "box.find('br').remove()\n",
    "print(box.text())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 其它"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<li class=\"item-0\">first item</li>\n",
      "        <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
      "        <li class=\"item-2 active\"><a href=\"link3.html\">\n",
      "            <span>third item</span>\n",
      "        </a></li>\n",
      "        <li class=\"item-3\"><a href=\"link2.html\">xxxxxxxxxxxxxxxxx item</a></li>\n",
      "        <li class=\"item-4 active\"><a href=\"link4.html\">fourth item</a></li>\n",
      "        <li class=\"item-5\"><a href=\"link5.html\">first item</a></li>\n",
      "    \n",
      "<Element li at 0x1a58fd0>\n",
      "<Element li at 0x1a6f3c8>\n",
      "<Element li at 0x1a6f058>\n",
      "<Element li at 0x1a6f3f0>\n",
      "<Element li at 0x1a6f440>\n",
      "<Element li at 0x1a6f418>\n",
      "----------------------------------------\n",
      "----------------------------------------\n",
      "<Element li at 0x1a58fd0>\n",
      "<Element li at 0x1a6f3c8>\n",
      "<Element li at 0x1a6f058>\n",
      "<Element li at 0x1a6f3f0>\n",
      "<Element li at 0x1a6f440>\n",
      "<Element li at 0x1a6f418>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "doc = pq(html)\n",
    "li= doc('li')\n",
    "print(li)\n",
    "def bar(i,v):\n",
    "    print(v)\n",
    "def bar2(i,v):\n",
    "    pass\n",
    "def bar3(i,v):\n",
    "    print(v)\n",
    "li.each(bar)\n",
    "print('--'*20)\n",
    "li.filter(bar2)\n",
    "print('--'*20)\n",
    "li.map(bar3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
